#include <filter/filterFactory.h>
#include <utitl/logger.h>

#include <dlfcn.h>

using namespace ECG::Utitl;

namespace ECG
{
    namespace Filter
    {
    FilterFactoryPtr FilterFactory::m_filterFactory;

    CFilterPtr FilterFactory::getFilterByID(std::string ID)
    {
        CFilterPtr filter;
        path += "lib" + ID + ".so";
        log_debug() << "filter path:" << path;

        void *handle = dlopen(path.c_str(), RTLD_LAZY);
        if (handle == NULL)
        {
            log_error() << "unable open file:" << path;
            return filter;
        }
        CFilterPtr (*createClass)() = (CFilterPtr(*)())dlsym(handle, "createClass");
        if (NULL == createClass)
        {
            log_error() << "unable find method: createClass";
            return filter;
        }
        filter = createClass();
        return filter;
    }

    FilterFactoryPtr FilterFactory::instance()
    {
        if(!m_filterFactory.get())
        {
            m_filterFactory.reset(new FilterFactory());
        }

        return m_filterFactory;
    }

    FilterFactory::FilterFactory() : path("/opt/ECG/lib/filter/")
    {

    }

    }
}